/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;


/**
 * The Class Di2UMLElementCreateCommand.
 * 
 * @author Patrick Tessier
 * 
 * this class is used to add the di2 element to another graph element
 */
public class Di2WithoutUMLAddCommand extends Command{

	/**
	 * The location.
	 */
	protected Point location=null;

	/**
	 * The size.
	 */
	protected Dimension size=null;

	
	/**
	 * The parent GE.
	 */
	protected GraphElement newParentGE;

	/**
	 * The parent GE.
	 */
	protected GraphElement oldParentGE;

	/**
	 * the created graphElement*.
	 */
	protected GraphElement childGe;
	protected NodeHelper nh;

	/**
	 * The Constructor.
	 * 
	 * @param element the element
	 * @param typeDiagram 
	 * @param childGe 
	 * @param parent the parent
	 */
	public Di2WithoutUMLAddCommand(GraphElement childGe, GraphElement parent,String typeDiagram) {
		nh= new NodeHelper();
		this.newParentGE=parent;
		this.childGe=childGe;

		this.oldParentGE=childGe.getContainer();

	
		this.setLabel("\"addition of graphElement \"");
	}

	/**
	 * use it when you manipulate a node with semantic parent if not the undo not run.
	 * 
	 * @param oldParent 
	 */
	public void setOldParent(GraphElement oldParent){
		this.oldParentGE=oldParent;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean canExecute() {
		
		return nh.canCreateGraphElement(this.newParentGE);
	}

	/**
	 * {@inheritDoc}
	 */
	public void execute(){
		nh.addGraphElement(this.childGe, this.newParentGE);
	}

	/**
	 * {@inheritDoc}
	 */
	public void redo(){
		nh.addGraphElement(this.childGe,this.newParentGE);
	}

	/**
	 * {@inheritDoc}
	 */
	public void undo(){
		//nh.addGraphElement(this.childGe, this.oldParentGE);
		nh.removeGraphElement(this.childGe, this.newParentGE);
	}

}
